//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.ComponentModel;
using System.Workflow.ComponentModel;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("URI Builder")]
    [Description("From the various fields that constitute the parts of a URI, create a URI object")]
    [WorkflowCategory("Utilities")]
    public partial class UriBuilder : System.Workflow.ComponentModel.Activity
    {
        #region Property definitions
        // Required input property
        public static DependencyProperty HostNameProperty =
            DependencyProperty.Register("HostName", typeof(string), typeof(UriBuilder),
            new PropertyMetadata("www.microsoft.com", DependencyPropertyOptions.Default));
        // Optional input properties
        public static DependencyProperty FragmentProperty =
            DependencyProperty.Register("Fragment", typeof(string), typeof(UriBuilder),
            new PropertyMetadata(string.Empty, DependencyPropertyOptions.Optional));
        public static DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(UriBuilder),
            new PropertyMetadata(string.Empty, DependencyPropertyOptions.Optional));
        public static DependencyProperty PathProperty =
            DependencyProperty.Register("Path", typeof(string), typeof(UriBuilder),
            new PropertyMetadata(string.Empty, DependencyPropertyOptions.Optional));
        public static DependencyProperty PortProperty =
            DependencyProperty.Register("Port", typeof(int), typeof(UriBuilder),
            new PropertyMetadata(80, DependencyPropertyOptions.Optional));
        public static DependencyProperty SchemeProperty =
            DependencyProperty.Register("Scheme", typeof(string), typeof(UriBuilder),
            new PropertyMetadata(string.Empty, DependencyPropertyOptions.Optional));
        public static DependencyProperty UserNameProperty =
            DependencyProperty.Register("UserName", typeof(string), typeof(UriBuilder),
            new PropertyMetadata(string.Empty, DependencyPropertyOptions.Optional));
        // Output property
        public static DependencyProperty UriProperty =
            DependencyProperty.Register("Uri", typeof(Uri), typeof(UriBuilder));

        [RequiredInputParam]
        [Name("Host Name", "Host")]
        [Description("Required host name (e.g. 'www.microsoft.com')")]
        public string HostName
        {
            get { return ((string)(base.GetValue(UriBuilder.HostNameProperty)));}
            set { base.SetValue(UriBuilder.HostNameProperty, value); }
        }

        [OptionalInputParam]
        [Name("Fragment")]
        [Description("Optional fragment portion of the Uri (text following the '#' symbol)")]
        public string Fragment
        {
            get { return ((string)(base.GetValue(UriBuilder.FragmentProperty))); }
            set { base.SetValue(UriBuilder.FragmentProperty, value); }
        }

        [OptionalInputParam]
        [Name("Password")]
        [Description("Optional password field")]
        public string Password
        {
            get { return ((string)(base.GetValue(UriBuilder.PasswordProperty))); }
            set { base.SetValue(UriBuilder.PasswordProperty, value); }
        }

        [OptionalInputParam]
        [Name("Path")]
        [Description("Optional path (e.g. 'index.html' or 'images/icon.gif')")]
        public string Path
        {
            get { return ((string)(base.GetValue(UriBuilder.PathProperty))); }
            set { base.SetValue(UriBuilder.PathProperty, value); }
        }

        [OptionalInputParam]
        [Name("Port")]
        [Description("Optional port field, defaults to 80")]
        public int Port
        {
            get { return ((int)(base.GetValue(UriBuilder.PortProperty))); }
            set { base.SetValue(UriBuilder.PortProperty, value); }
        }

        [OptionalInputParam]
        [Name("Scheme")]
        [Description("Optional scheme field, defaults to 'http'")]
        public string Scheme
        {
            get { return ((string)(base.GetValue(UriBuilder.SchemeProperty))); }
            set { base.SetValue(UriBuilder.SchemeProperty, value); }
        }

        [OptionalInputParam]
        [Name("User Name", "User")]
        [Description("Optional user name field")]
        public string UserName
        {
            get { return ((string)(base.GetValue(UriBuilder.UserNameProperty))); }
            set { base.SetValue(UriBuilder.UserNameProperty, value); }
        }

        [OutputParam]
        [Name("URI")]
        [Description("The Uri created by this activity")]
        public Uri Uri
        {
            get { return ((Uri)(base.GetValue(UriBuilder.UriProperty))); }
        }
        #endregion

        public UriBuilder()
		{
			InitializeComponent();
		}

        /// <summary>
        /// Creates the Uri from its various parts
        /// </summary>
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            System.UriBuilder baseBuilder = new System.UriBuilder();
            
            baseBuilder.Host = this.HostName;
            baseBuilder.Port = this.Port;
            
            if (!string.IsNullOrEmpty(this.Fragment))
                baseBuilder.Fragment = this.Fragment;
            if (!string.IsNullOrEmpty(this.Password))
                baseBuilder.Password = this.Password;
            if (!string.IsNullOrEmpty(this.Path))
                baseBuilder.Path = this.Path;
            if (!string.IsNullOrEmpty(this.Scheme))
                baseBuilder.Scheme = this.Scheme;
            if (!string.IsNullOrEmpty(this.UserName))
                baseBuilder.UserName = this.UserName;

            base.SetValue(UriBuilder.UriProperty, baseBuilder.Uri);

            return ActivityExecutionStatus.Closed;
        }
	}
}
